import * as constants from './constants'
import api from '../../../api/api'
import Immutable from 'immutable';
import { actionCreators } from '../../../components/header/store/'
import {getLesson } from '../../home/store/actionCreators'
import { message } from 'antd';
import { push,go } from 'react-router-redux';

const changePay = (result) => ({
  type: constants.CHANGE_PAY,
  value: Immutable.fromJS(result)
})

const changePayType = (result) => ({
  type: constants.CHANGE_PAY_TYPE,
  value: result
})

let payTime

export const removeCart = (params) => (dispatch) => {
  api.removeCart(params).then(res => {
    console.log('res', res)
    if(res.code === 20000) {
      message.success('移除购物车成功！')
      dispatch(actionCreators.getCartList())
      dispatch(getLesson({eduRank: '小学', type: 'all'}))
      dispatch(getLesson({eduRank: '初中', type: 'all'}))
      dispatch(getLesson({eduRank: '高中', type: 'all'}))
    }
  })
}

export const pay = (params) => async (dispatch) => {
  // dispatch(push({pathname: 'mycourse'}))
  api.pay(params).then(res => {
    // console.log('res', res)
    if(res.code === 20000) {
      dispatch(changePay(res.data))
      dispatch(changePayType(true))

      payTime = setInterval(()=> {
        api.tradeOk({
          outTradeNo: res.data.outTradeNo
        }).then(res => {
          if(res.code === 20000) {
            message.success('支付成功！')
            dispatch(changeModal(false))
            setTimeout(()=> {
              window.location.href="/"
              // dispatch(push('/'))
            }, 2000)
          }
        })
      }, 2000)
    }
  })
}


export const changeModal = (params) => {
  clearInterval(payTime)
  return (changePayType(params))
}

const clickA = (url, target) => {
  let a = document.createElement('A')
  a.setAttribute('id', 'opennewpanel')
  a.style.display = 'none'
  a.setAttribute('href', url)
  if(target) {
    a.setAttribute('target', '_blank')
  }
  document.body.appendChild(a)

  let ma = document.getElementById('opennewpanel')
  ma.click()
  ma.remove()
}

// 手机使用
export const toPay = (params) => (dispatch) => {
  api.pay(params).then(res => {
    // console.log('res', res)
    if(res.code === 20000) {
      dispatch(changePay(res.data))
      dispatch(changePayType(true))
      // 支付宝
      if(params.payMethod == 0) {
        let obj = document.createElement("div");
        obj.setAttribute("id", "newpay");
        obj.style.display = "none";
        obj.innerHTML = res.data.aliPay;
        document.body.appendChild(obj);
        setTimeout(function() {
          obj.children[0].submit();
        }, 200);
      }else if(params.payMethod == 1) {
        // window.open(res.data.wechatPay)
        // window.location.href= res.data.wechatPay
        var u = navigator.userAgent;
        var isAndroid = u.indexOf('Android') > -1 || u.indexOf('Adr') > -1; //android终端
        var isiOS = !!u.match(/\(i[^;]+;( U;)? CPU.+Mac OS X/); //ios终端
        if(isiOS) {
          window.location.href= res.data.wechatPay
        }else {
          clickA(res.data.wechatPay, true)
        }
      }
    }
  })
  // debugger
  // clickA('https://www.baidu.com', true)
}

export const clearAll = () => (dispatch) => {
  api.clearAllCart().then(res=> {
    if(res.code === 20000) {
      message.success('清空购物车成功！')
      dispatch(actionCreators.getCartList())
    }
  })
}
